package com.example.administrator.szb.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.util.Toasts;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by D N on 2017/12/6.
 */

public class TestActivity extends BaseActivity {
    @Bind(R.id.text_web)
    WebView textWeb;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);
        ButterKnife.bind(this);

        textWeb.getSettings().setJavaScriptEnabled(true);
//    添加js对象(必要)
        textWeb.addJavascriptInterface(new JavaScriptInterface(this), "loginActionAndroid");
//        textWeb.setWebChromeClient(new WebChromeClient());
//        textWeb.setWebViewClient(new WebViewClient() {
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                testControl();
//            }
//        });
        textWeb.loadUrl("file:///android_asset/test.html");
    }


    private void testControl() {
        String control = "javascript:helloJava()";
        control = "javascript:helloJavaWithParam(\""+"param1"+"\")";
        control = "javascript:helloToJava()";
        textWeb.loadUrl(control);
    }

    static class JavaScriptInterface  {
        //    测试方法
        Activity activity;

        public JavaScriptInterface(Activity activity) {
            this.activity = activity;
        }
        @JavascriptInterface
        public void postMessage(Object o) {
            Toasts.show(activity,"test", Toast.LENGTH_SHORT);
        }
    }
}
